import { drugsDelReq, drugsSearchReq, drugsBatchdelReq, drugsAddReq, recipelListReq, recipelInfobyidReq, infoRevenueReq, drugsEditReq } from '@/api/ware.api'
import { Module } from 'vuex'
import { ElMessage } from 'element-plus'

type waretype = {
    total: number | null,
    drugList: [],
    recipelList: [],
    recipelTotal: number | null,
    recipelInfo: [],
    price: number | null,
    information: [],
    departmentChart:{},
    weightChart:{}
}

const userModule: Module<waretype, any> = {
    // vuex 可定要提供一个接口，用来描述每一个模块
    state: {
        total:null,
        drugList:[],
        recipelList:[],
        recipelTotal:null,
        recipelInfo:[],
        price:null,
        information:[],
        departmentChart:{},
        weightChart:{}
    },
    namespaced: true,
    mutations:{
        M_drugList(state,payload){
            state.drugList = payload
        },
        M_total(state,payload){
            state.total = payload
        },
        M_recipelList(state,payload){
            state.recipelList = payload
        },
        M_recipelTotal(state,payload){
            state.recipelTotal = payload
        },
        M_recipelInfo(state,payload){
            state.recipelInfo = payload
        },
        M_price(state,payload){
            state.price = payload
        },
        M_information(state,payload){
            state.information = payload
        },
        M_weightChart(state,payload){
            state.weightChart = payload
        },
        M_departmentChart(state,payload){
            state.departmentChart = payload
        },
    },
    actions:{
        // 获取药品数据列表
        async A_drugList({commit},payload){
            const res = await drugsSearchReq(payload)
            // 添加标杆
            const data = res.data.data
            data.forEach(item => {
                item.flag = 0
            });
            console.log(data);
            
            // 判断是否查询成功
            if(res.data.code === 1){
                commit('M_drugList',data)
                commit('M_total',res.data.total)
            }else{
                return res.data.msg
            }
        },
        //删除单个医疗药品
        async A_drugDel({commit},payload){
            const res = await drugsDelReq(payload)
            console.log(res);
            if(res.data.code === 1){
                // commit('M_drugList',res.data.data)
                // commit('M_total',res.data.total)
                ElMessage({
                    message: res.data.msg,
                    type: 'success',
                  })
            }else{
                ElMessage.error(res.data.msg)
            }
            
        },
        // 批量删除医疗药品
        async A_drugsBatchdel(ctx,payload){
            const res = await drugsBatchdelReq(payload)
            console.log(res);
             if(res.data.code === 1){
                ElMessage({
                    message: res.data.msg,
                    type: 'success',
                  })
            }else{
                ElMessage.error(res.data.msg)
            }
            
        },
        // 添加医品
        async A_drugAdd({commit},payload){
            const res = await drugsAddReq(payload)
            console.log(res);
            if(res.data.code === 1){
                ElMessage({
                    message: res.data.msg,
                    type: 'success',
                  })
            }else{
                ElMessage.error(res.data.msg)
            }            
        },
        // 修改医疗药品
        async A_drugEdit({commit},payload){
            const res = await drugsEditReq(payload)
            console.log(res); 
            if(res.data.code === 1){
                ElMessage({
                    message: res.data.msg,
                    type: 'success',
                  })
            }else{
                ElMessage.error(res.data.msg)
            }
        },
        // 获取全科室处方列表
        async A_recipelList({commit},payload){
            const res = await recipelListReq(payload)
            console.log(res);
            commit('M_recipelList',res.data.data)
            commit('M_recipelTotal',res.data.total)
        },
        // 处方详情
        async A_recipelInfo({commit},payload){
            const res = await recipelInfobyidReq(payload)
            console.log(res);
            commit('M_recipelInfo',res.data.data)
            commit('M_price',res.data.data[0].price)
        },
        // 营收大数据
        async A_information({commit}){
            const res = await infoRevenueReq()
            console.log(res);
            commit('M_information',res.data.table)
            commit('M_weightChart',res.data.weightChart)
            commit('M_departmentChart',res.data.departmentChart)
        }
    }
}

export default userModule